package com.hsy.leancloudchat.mvvmUtils

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class HsyFragment<VB : ViewBinding, VM : HsyViewModel> : Fragment() {
    protected val mBinding by lazy {
        createViewBinding()!!
    }

    protected val mViewModel by lazy {
        createViewModel()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.v(TAG, "onAttach")

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.v(TAG, "onCreate")

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.v(TAG, "onCreateView")
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.v(TAG, "onViewCreated")

    }

    override fun onStart() {
        super.onStart()
        Log.v(TAG, "onStart")

    }

    override fun onResume() {
        super.onResume()
        Log.v(TAG, "onResume")

    }

    override fun onPause() {
        super.onPause()
        Log.v(TAG, "onPause")

    }

    override fun onStop() {
        super.onStop()
        Log.v(TAG, "onStop")

    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.v(TAG, "onDestroyView")

    }

    override fun onDestroy() {
        super.onDestroy()
        Log.v(TAG, "onDestroy")

    }

    override fun onDetach() {
        super.onDetach()
        Log.v(TAG, "onDetach")

    }

    /**
     * 通过反射创建 viewBinding
     * @return  ViewBinding
     */
    open fun createViewBinding(): VB? {
        return createViewBinding(layoutInflater)
    }

    /**
     * 通过反射创建 viewModel
     * @return  ViewModel
     */
    open fun createViewModel(): VM {
        return createViewModel(this)
    }
}